package com.example.administrator.szb.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import com.example.administrator.szb.bean.DownIngBean;
import com.example.administrator.szb.http.okhttps.DownloadUtil;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.UpdateAppService;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * Created by stone on 2018/2/7.
 */

public class UpdateAppServices extends IntentService {
    public static boolean isStart = false;
    private Context context2;
    DownIngBean downIngBean;
    //EventBus.getDefault().post(downIngBean);
    @Override
    public void onCreate() {
        super.onCreate();
        isStart = true;
        context2 = this;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isStart = false;
        downIngBean = new DownIngBean();
        downIngBean.setProgress(0);
        downIngBean.setPath(null);
        downIngBean.setUrl(null);
        downIngBean.setIsFail(0);
    }

    public UpdateAppServices() {
        super("UpdateAppServices");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent!=null){
            String url = intent.getStringExtra("url");
            downStart(url);
        }
    }
    private  void downStart(String url){
        UpdateAppService.isDowning = true;
        DownloadUtil.get().download(url, "szbfile", new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(String path) {
                UpdateAppService.isDowning = false;
                if (downIngBean==null){
                    downIngBean = new DownIngBean();
                }
                downIngBean.setProgress(100);
                downIngBean.setPath(path);
                downIngBean.setUrl(null);
                downIngBean.setIsFail(0);
                EventBus.getDefault().post(downIngBean);
                //bottom_tips.setText("立即安装");

                try {
                    Thread.sleep(1000);
                    install(path);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    install(path);
                }
            }
            @Override
            public void onDownloading(int progress) {
                UpdateAppService.isDowning = true;
                if (downIngBean==null){
                    downIngBean = new DownIngBean();
                }
                downIngBean.setProgress(progress);
                downIngBean.setPath(null);
                downIngBean.setUrl(null);
                downIngBean.setIsFail(0);
                EventBus.getDefault().post(downIngBean);
                //progressBar.setProgress(progress);
            }
            @Override
            public void onDownloadFailed() {
                UpdateAppService.isDowning = false;
                if (downIngBean==null){
                    downIngBean = new DownIngBean();
                }
                downIngBean.setProgress(0);
                downIngBean.setPath(null);
                downIngBean.setUrl(null);
                downIngBean.setIsFail(9);
                EventBus.getDefault().post(downIngBean);
                //DialogUtil.showToast(context, "下载失败");
            }
        });
    }


    /**
     * 通过隐式意图调用系统安装程序安装APK
     */
    public   void install( String apkPath) {
        if (TextUtils.isEmpty(apkPath)) {
            return;
        }
        QTLog.e(apkPath);

        File file = new File(apkPath);
        Intent intent = new Intent(Intent.ACTION_VIEW);

        //判读版本是否在7.0以上
        if (Build.VERSION.SDK_INT >= 24) {
            //provider authorities
            Uri apkUri = FileProvider.getUriForFile(context2, "com.example.administrator.szb.MydemoFileProvider", file);
            //Granting Temporary Permissions to a URI
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        QTLog.e(apkPath);
        context2.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
